﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Tools.Publishing;
using $safeprojectname$.Infrastructure.Factories;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace $safeprojectname$.Publishing.Handlers
{
    [TestClass]
    public class CreatingExceptionHandlerTest : TestBase
    {
        [TestMethod]
        public void MissingHandlerSettings_Failed()
        {
            Exception exception = null;

            try
            {
                var provider = ProviderFactory.ProviderExceptionWithMissingHandlerSettings();
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }

            // the default config file path set in PublishingConfigurator
            Assert.IsTrue(exception.Message.Contains("The provided file 'Config/Publishing.config' either does not exists, or cannot be loaded"), exception.Message);

        }
        // TODO [TestMethod]
        public void InitCallWithNullElement_Failed()
        {
            Exception exception = null;

            try
            {
                var provider = new ExceptionPublishingHandler();
                provider.Init(null);
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            // Contracts, XElement cannot be null
            Assert.IsTrue(exception.Message.Contains("Precondition failed: settings.Is()  Parameter 'settings', passed into an IPublishingHandler Init() method cannot be null"), exception.Message);
        }
        [TestMethod]
        public void IncorrectInitCall_Failed()
        {
            Exception exception = null;

            try
            {
                var provider = new ExceptionPublishingHandler();
                provider.Init(new XElement("settings"));
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }

            // path from SectionExample.config
            Assert.IsTrue(exception.Message.Contains("The provided file 'Config/Publishing.config' either does not exists, or cannot be loaded"), exception.Message);
        }

        [TestMethod]
        public void WrongConfigPath_Failed()
        {
            Exception exception = null;

            try
            {
                var provider = ProviderFactory.ProviderExceptionWrongConfigPath();
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }

            // path from SectionExample.config
            Assert.IsTrue(exception.Message.Contains("The provided file 'Publishing.configwrong' either does not exists, or cannot be loaded (full"), exception.Message);
        }

        #region Init

        // You can use the following additional attributes as you write your tests:

        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }

        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }

        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion Init
    }
}
